﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using NetworkSupervisor.Helper;
using NetworkSupervisor.Model.NetworkSupervisor;
using System.Linq;

namespace NetworkSupervisor
{
    public partial class FormQQDataManager : DevExpress.XtraEditors.XtraForm
    {
        public FormQQDataManager()
        {
            InitializeComponent();
        }

        private void dataNavigator1_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            switch (e.Button.ButtonType)
            {
                case NavigatorButtonType.Custom:
                    break;
                case NavigatorButtonType.First:
                    break;
                case NavigatorButtonType.PrevPage:
                    break;
                case NavigatorButtonType.Prev:
                    break;
                case NavigatorButtonType.Next:
                    break;
                case NavigatorButtonType.NextPage:
                    break;
                case NavigatorButtonType.Last:
                    break;
                case NavigatorButtonType.Append:
                    break;
                case NavigatorButtonType.Remove:
                    e.Handled = true;
                    if (gridView1.RowCount < 1) break;
                    DialogResult result = XtraMessageBox.Show("是否确认删除QQ，并同时删除QQ在线记录？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (result == DialogResult.OK)
                    {
                        taQQ列表 item = gridView1.GetFocusedRow() as taQQ列表;
                        if (item.QQ != "")
                        {
                            session1.ExecuteNonQuery(string.Format("DELETE FROM taQQ在线纪录 WHERE QQ='{0}'", item.QQ));
                            item.Delete();
                        }
                    }
                    break;
                case NavigatorButtonType.Edit:
                    break;
                case NavigatorButtonType.EndEdit:
                    XtraMessageBox.Show("保存成功");
                    break;
                case NavigatorButtonType.CancelEdit:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            GridExportHelper.ExportGridToExcel(gridControl1, "QQ列表");
        }

        private Helper.IExcelHelper excelHelper = null;
        private IEnumerable<taQQ列表> qqCollection = null;
        private FormLoadProgress formProgress = null;

        private void btnImport_Click(object sender, EventArgs e)
        {
            qqCollection = XPCQQ列表.Cast<taQQ列表>();
            OpenFileDialog f = new OpenFileDialog();
            f.Filter = "Excel(*.xls)|*.xls";
            if (f.ShowDialog() == DialogResult.OK)
            {
                if (XtraMessageBox.Show("导入的文件名为:" + f.FileName + "\r\n是否确认导入数据？", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }

                excelHelper = new Helper.ExcelHelper(f.FileName);
                if (!(excelHelper.ColumnList.Contains("单位") && excelHelper.ColumnList.Contains("姓名") && excelHelper.ColumnList.Contains("QQ") && excelHelper.ColumnList.Contains("标识号") && excelHelper.ColumnList.Contains("标记")))
                {
                    XtraMessageBox.Show("请确保导入的EXCEL包含以下列名：单位、姓名、QQ、标识号、标记");
                    return;
                }
                formProgress = new FormLoadProgress();
                formProgress.mySetInfo("正在导入数据，请稍等...");
                formProgress.mySetProgressMaximum(excelHelper.RowCount);

                System.Threading.ThreadPool.QueueUserWorkItem(callBack);
            }
        }

        private void callBack(object state)
        {
            int rowCount = excelHelper.RowCount;
            this.UIThreadBeginInvoke(() => formProgress.ShowDialog());
            for (int i = 1; i < rowCount; i++)
            {
                string dw = excelHelper.GetCellValue(i, "单位");
                string xm = excelHelper.GetCellValue(i, "姓名");
                string qq = excelHelper.GetCellValue(i, "QQ");
                string bsh = excelHelper.GetCellValue(i, "标识号");
                string bj = excelHelper.GetCellValue(i, "标记");
                taQQ列表 qqItem = qqCollection.FirstOrDefault(o => o.QQ == qq);
                if (qqItem == null)
                {
                    this.UIThread(() =>
                                      {
                                          taQQ列表 item = new taQQ列表(session1);
                                          item.QQ = qq;
                                          item.单位 = dw;
                                          item.姓名 = xm;
                                          item.标识号 = bsh;
                                          item.标记 = bj;
                                          item.Save();
                                          XPCQQ列表.Add(item);
                                      });
                }
                else
                {
                    this.UIThread(() =>
                                      {
                                          qqItem.QQ = qq;
                                          qqItem.单位 = dw;
                                          qqItem.姓名 = xm;
                                          qqItem.标识号 = bsh;
                                          qqItem.标记 = bj;
                                          qqItem.Save();
                                      });
                }
                this.UIThread(() => formProgress.mySetProgressPosition(i + 1));
            }
            this.UIThread(() => formProgress.myClose());

            this.UIThread(() => XtraMessageBox.Show("数据导入成功"));
        }

        private void FormQQDataManager_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }
    }
}